package pl.iachoo.ctl.tabory.dao.core.mybatis.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pl.iachoo.ctl.tabory.dao.api.services.IUserRoleService;
import pl.iachoo.ctl.tabory.dao.core.mybatis.mapper.UserRoleMapper;
import pl.iachoo.ctl.tabory.dao.data.vo.UserRoleVO;

public class UserRoleServiceImpl implements IUserRoleService {

	private final static Logger logger = Logger
			.getLogger(UserRoleServiceImpl.class);

	private SqlSessionFactory sessionFactory;

	public UserRoleServiceImpl(SqlSessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<UserRoleVO> getAllUserRoles() throws Exception {
		SqlSession session = sessionFactory.openSession();
		List<UserRoleVO> allRoles = new ArrayList<UserRoleVO>();
		try {
			UserRoleMapper mapper = session.getMapper(UserRoleMapper.class);
			allRoles = mapper.getAllRoles();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return allRoles;
	}

	@Override
	public UserRoleVO getUserRole(Long userRoleId) throws Exception {
		SqlSession session = sessionFactory.openSession();
		UserRoleVO role = null;
		try {
			UserRoleMapper mapper = session.getMapper(UserRoleMapper.class);
			role = mapper.getById(userRoleId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return role;
	}

}
